package com.cango.qutz.web.auth.menu.service.impl;

import java.util.List;

import com.cango.qutz.web.auth.menu.dao.IMenuDao;
import com.cango.qutz.web.auth.menu.service.IMenuService;
import com.cango.qutz.web.auth.resource.dao.IResourceDao;
import com.cango.qutz.web.entity.MenuEntity;
import com.cango.framework.base.pagination.PageInfo;
import com.cango.framework.base.pagination.PaginationResult;
import com.cango.qutz.web.auth.menu.dao.IMenuDao;
import com.cango.qutz.web.auth.menu.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * 〈一句话功能简述〉<br>
 * 菜单服务组件
 * 
 * @author Administrator
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
@Service("menuService")
public class MenuServiceImpl implements IMenuService {
    /** 系统菜单sql的命名空间 */
    private static final String SQL_NAMESPACE = "menu.";

    /** 新增系统菜单的sql id */
    private static final String SQL_INSERT = "insertMenu";

    /** 更新系统菜单的sql id */
    private static final String SQL_UPDATE = "updateMenu";

    /** 删除系统菜单的sql id */
    private static final String SQL_DELETE = "deleteMenu";

    /** 根据主键查询系统菜单的sql id */
    private static final String SQL_QUERY_BY_ID = "queryMenuById";

    /** 查询系统菜单的sql id */
    private static final String SQL_QUERY_FOR_LIST = "queryMenus";

    /** 分页查询系统菜单的sql id */
    private static final String SQL_QUERY_BY_PAGE = "queryMenusByPage";
    /** 根据父Id查找子菜单 */
    private static final String SQL_QUERY_FOR_LIST_BYPARENTCODE = "queryMenuByParentCode";

    /**
     * 根据菜单查询数量
     */
    private static final String SQL_QUERY_COUNT_BY_MENU = "queryCountByMenu";

    /**
     * 根据按钮查询数量
     */
    private static final String SQL_QUERY_COUNT_BY_BUTTON = "queryCountByButton";

    /** 菜单menudao组件 */
    @Autowired
    private IMenuDao menuDao;

    /** resourcedao组件 */
    @Autowired
    private IResourceDao resourceDao;

    @Override
    public int insert(MenuEntity menu) {
        String menuPath = null;
        // 根据父级菜单查询从菜单信息
        if (null != menu.getParentCode() && !"".equals(menu.getParentCode())) {
            MenuEntity menuParent = this.queryObjectById(menu.getParentCode());
            menuPath = menuParent.getMenuPath() + ">>" + menu.getMenuCode();
        } else {
            menuPath = ">>" + menu.getMenuCode();
        }
        menu.setMenuPath(menuPath);
        int id = menuDao.insert(SQL_NAMESPACE + SQL_INSERT, menu);
        return id;
    }

    @Override
    public int update(MenuEntity menu) {
        String menuPath = null;
        // 根据父级菜单查询从菜单信息
        if (null != menu.getParentCode() && !"".equals(menu.getParentCode())) {
            MenuEntity menuParent = this.queryObjectById(menu.getParentCode());
            menuPath = menuParent.getMenuPath() + ">>" + menu.getMenuCode();
        } else {
            menuPath = ">>" + menu.getMenuCode();
        }
        menu.setMenuPath(menuPath);
        return menuDao.update(SQL_NAMESPACE + SQL_UPDATE, menu);
    }

    @Override
    public int delete(MenuEntity menu) {
        return menuDao.delete(SQL_NAMESPACE + SQL_DELETE, menu);
    }

    @Override
    public MenuEntity queryObjectById(String menuCode) {
        return menuDao.queryObjectById(SQL_NAMESPACE + SQL_QUERY_BY_ID, menuCode);
    }

    @Override
    public List<MenuEntity> queryList(Object param) {
        return menuDao.queryList(SQL_NAMESPACE + SQL_QUERY_FOR_LIST, param);
    }

    @Override
    public PaginationResult<MenuEntity> queryListByPage(MenuEntity param, PageInfo pageinfo) {
        return menuDao.queryListByPage(SQL_NAMESPACE + SQL_QUERY_BY_PAGE, param, pageinfo);
    }

    @Override
    public List<MenuEntity> queryListByParentCode(Object param) {
        return menuDao.queryList(SQL_NAMESPACE + SQL_QUERY_FOR_LIST_BYPARENTCODE, param);
    }

    @Override
    public boolean validMenuCode(String menuCode) {
        int count = this.menuDao.queryCountByMenuCode(SQL_NAMESPACE + SQL_QUERY_COUNT_BY_MENU, menuCode);
        return count > 0 ? false : true;
    }

    @Override
    public boolean validButtonCode(String buttonCode) {
        int count = this.menuDao.queryCountByMenuCode(SQL_NAMESPACE + SQL_QUERY_COUNT_BY_BUTTON, buttonCode);
        return count > 0 ? false : true;
    }

}
